package com.jt.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class SpringAOP {

    //@Pointcut("bean(useServiceImpl)")
    //@Pointcut("within(com.jt.service.UseServiceImpl)")
    //@Pointcut("within(com.jt.service.*)")
    //@Pointcut("within(com.jt.service..*)")
    //@Pointcut("within(com.*.service..*)")
    //@Pointcut("execution(* com.jt.service.UseServiceImpl.say())")
    //@Pointcut("execution(* com.jt.service..*.*(..))")
    @Pointcut("@annotation(com.jt.anno.Lyl)")
    public void pointcut() {


    }

    //@Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        System.out.println("我是前置通知");
        Class targetClass = joinPoint.getTarget().getClass();
        String methodName = joinPoint.getSignature().getName();
        String className = joinPoint.getSignature().getDeclaringTypeName();
        Object[] objs = joinPoint.getArgs();
        System.out.println(targetClass);
        System.out.println(methodName);
        System.out.println(className);
        System.out.println(Arrays.toString(objs));
    }

    //@AfterThrowing(value = "pointcut()",throwing = "exception")
    public void afterThrowing(Exception exception){
        System.out.println("我是异常通知");
        System.out.println(exception.getMessage());
        System.out.println(exception);
        exception.printStackTrace();
    }

    //@After(value = "pointcut()")
    public void after(){
        System.out.println("我是最终通知");
    }

    @Around("pointcut()")
    @Order(2)
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知开始前");
        joinPoint.proceed();
        System.out.println("环绕通知开始后");
    }
}

